/*
  This file is the entry of the server. 

*/


/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
//var user = require('./routes/user');
var http = require('http');
var path = require('path');
var partials = require('express-partials');

var MongoStore = require('connect-mongostore')(express);
var domain = require('domain');

//private modules dependences
var locals = require("./modules/locals.js");//static helpers for the site
//var dynamicHelper = require("./modules/dynamic.js");//dynamic helpers for the site
var settings   = require("./settings");//configurences of the server



//the store object will now be used to store the session information.
//As the name mentioned,the data will be stored in mongodb.
var store = new MongoStore({
    host: settings.sessionHost, 
    　　 port: settings.sessionPort, 
    db: settings.sessionDb
});


//
//
//-----------------------
//server start code

var app = express();

// all environments
// configure from start up

//app.configure(site_configure());
site_configure();

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

//add route mapping
routes.route(app);

//add static helper to use in the ejs page
app.locals(locals);

//start the server
http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

//-----------------------
//--that's end the start-
//-----------------------


//site configurition file
function site_configure(){
    
    //set the init static information
    //set the listening port,default will be 3000.
    app.set('port', process.env.PORT || 3000);
    
    //set where to look at the view template.
    app.set('views', path.join(__dirname, 'views'));
    
    //will use the ejs as the view engine
    app.set('view engine', 'ejs');

    //------set the middlewares------
    app.use(express.favicon());    
    app.use(express.logger('dev'));
    /*
      Request body parsing middleware supporting JSON, urlencoded, and multipart requests.
      This middleware is simply a wrapper the json(), urlencoded(), and multipart() middleware.
    */
    app.use(express.bodyParser());
    
/*
  Parses the Cookie header field and populates req.cookies with an object keyed by the cookie names. 
  Optionally you may enabled signed cookie support by passing a secret string.
*/
    
    app.use(express.cookieParser());

    app.use(express.methodOverride());

/*
    app.use(express.session({
	secret : settings.cookieSecret,
	store : store
    }));       
*/
    
    app.use(partials());//!ythis must be used before app.route
    
    app.use(app.router);

    //static file path,include javascripts and images,etc    
    app.use(express.static(path.join(__dirname, 'public')));
    
}



